package com.billing.web.controller.administrative;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ObjectUtils.Null;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.billing.model.entity.TAssets;
import com.billing.model.entity.TAssetsReminding;
import com.billing.model.entity.TAssetsdetail;
import com.billing.model.entity.TAssetsstorage;
import com.billing.model.entity.TUser;
import com.billing.service.AssetsRemindingService;
import com.billing.service.AssetsService;
import com.billing.service.AssetsdetailService;
import com.billing.service.AssetsstorageService;
import com.billing.service.UserService;


/**
 * 资产管理
 * @author Administrator
 *
 */
@RequestMapping(value="/assets")
@Controller
public class AssetsController {

	
	@Resource
	private AssetsService assetsService;
	@Resource
	private AssetsstorageService assetsstorageService;
	@Resource
	private AssetsdetailService assetsdetailService;
	@Resource
	private UserService userService;
	@Resource
	private AssetsRemindingService assetsRemindingService;
	
	
	SimpleDateFormat mdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 获取资产列表信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(HttpServletRequest request, Model model){
		String name = request.getParameter("name");
		String coding = request.getParameter("coding");
		int pageSize = 10;
		try {
			if (coding!=null&&coding.length() > 0) {
				coding = URLDecoder.decode(URLDecoder.decode(coding, "UTF-8"),"UTF-8");
			}
			if (name!=null&&name.length() > 0) {
				name = URLDecoder.decode(URLDecoder.decode(name, "UTF-8"),"UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
		}
		request.setAttribute("name", name);
		request.setAttribute("coding", coding);
		// 获取当前页数
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int organizationid=(int) request.getSession().getAttribute("organizationid");
		int totalNum = assetsService.getTAssetsNum(name, coding,organizationid);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<TAssets> list=assetsService.getTAssetsList(name,coding,organizationid, (currentPage - 1) * pageSize, pageSize);
		request.setAttribute("totalNum", totalNum);
		request.setAttribute("list", list);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalNum", totalNum);
        request.setAttribute("totalPage", totalPage);
		return "administrative/index";
	}
	
	/**
	 * 进入入库界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/insertAssets",method=RequestMethod.GET)
	public String insertAssets(HttpServletRequest request, Model model){
		return "administrative/insertAssets";
	}
	
	
	/**
	 * 入库
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public String insert(HttpServletRequest request, Model model){
//		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String coding = request.getParameter("coding");
		String number = request.getParameter("number");
		String unit = request.getParameter("unit");
		String models = request.getParameter("model");
		String unitprice = request.getParameter("unitprice");
		String totalprice = request.getParameter("totalprice");
		int organizationid=(int) request.getSession().getAttribute("organizationid");
		TAssets ass=assetsService.getTAssetsCoding(coding);
		int assid=0;
		if(ass!=null){
			if(number!=null&&!"".equals(number)){
				ass.setNumber(ass.getNumber()+Integer.valueOf(number));
			}
			assetsService.updateTAssets(ass);
			assid=ass.getId();
		}else{
			ass=new TAssets();
			ass.setName(name);
			ass.setCoding(coding);
			ass.setModel(models);
			ass.setNumber(Integer.valueOf(number));
			ass.setUnit(unitprice);
			ass.setUnit(unit);
			ass.setOrganizationId(organizationid);
			assetsService.insertTAssets(ass);
			assid=ass.getId();
		}
		TAssetsstorage age=new TAssetsstorage();
		age.setAssetsId(assid);
		age.setCreatetime(System.currentTimeMillis());
		age.setNumber(Integer.valueOf(number));
		age.setUnitprice(Integer.valueOf(unitprice));
		age.setTotalprice(Integer.valueOf(totalprice));
		int fal=assetsstorageService.insertTAssetsstorage(age);
		if(fal>0){
			request.setAttribute("msg", "新增成功!");
		}else{
			request.setAttribute("msg", "新增失败!");
		}
		return "administrative/insertAssets";
	}
	
	
	/**
	 * 入库明细
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/storagedetail",method=RequestMethod.GET)
	public String storagedetail(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		int pageSize = 10;
		request.setAttribute("id", id);
		// 获取当前页数
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int totalNum = assetsstorageService.getTAssetsstorageNum(Integer.valueOf(id));
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<TAssetsstorage> list=assetsstorageService.getTAssetsstorageList(Integer.valueOf(id), (currentPage - 1) * pageSize, pageSize);
		request.setAttribute("totalNum", totalNum);
		request.setAttribute("list", list);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalNum", totalNum);
        request.setAttribute("totalPage", totalPage);
		return "administrative/storagedetail";
	}
	
	
	/**
	 * 删除资产
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deleteAssets",method=RequestMethod.POST)
	public String deleteAssets(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		if(id!=null&&id!=""){
			assetsService.deleteTAssets(Integer.valueOf(id));
			assetsstorageService.deleteTAssetsstorage(Integer.valueOf(id));
		}
		return index(request, model);
	}
	
	/**
	 * 资产出库
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/outboundAssets",method=RequestMethod.GET)
	public String outboundAssets(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		TAssets assets=assetsService.getTAssetsbyId(Integer.valueOf(id));
		request.setAttribute("assets", assets);
		return "administrative/outboundAssets";
	}
	
	/**
	 * 资产出库
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/insertAssetsdetail",method=RequestMethod.POST)
	public String insertAssetsdetail(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		String userid = request.getParameter("userid");
		String number = request.getParameter("number");
		TAssetsdetail ass=new TAssetsdetail();
		ass.setUserId(Integer.valueOf(userid));
		ass.setAssetsId(Integer.valueOf(id));
		ass.setNumber(Integer.valueOf(number));
		ass.setCreatetime(System.currentTimeMillis());
		int fal=assetsdetailService.insertTAssetsdetail(ass);
		TAssets assets=assetsService.getTAssetsbyId(Integer.valueOf(id));
		if(assets!=null){
			assets.setNumber(assets.getNumber()-Integer.valueOf(number));
			assetsService.updateTAssets(assets);
		}
		if(fal>0){
			request.setAttribute("msg", "出库成功!");
		}else{
			request.setAttribute("msg", "出库失败!");
		}
		return "administrative/outboundAssets";
	}
	/**
	 * 设置提醒
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/updateReminding",method=RequestMethod.POST)
	public String updateReminding(HttpServletRequest request, Model model){
		String asid = request.getParameter("asid");
		String handleTime = request.getParameter("handleTime");
		String remindingContent = request.getParameter("remindingContent");
		String isonly = request.getParameter("isonly");
		String remindingCycle = request.getParameter("remindingCycle");
		String remindingInterval = request.getParameter("remindingInterval");
		TAssetsReminding assetsReminding = new TAssetsReminding();
		assetsReminding.setRemindingContent(remindingContent);
		assetsReminding.setAssetsId(Integer.parseInt(asid));
		assetsReminding.setRemindingType(Integer.parseInt(isonly));
		if(asid == null || asid.equals("")){
			request.setAttribute("msg", "资产id不能为空!");
			return "administrative/setReminding";
		}else if(remindingContent == null || remindingContent.equals("")){
			request.setAttribute("msg", "提醒内容不能为空!");
			return "administrative/setReminding";
		}else if(isonly == null || isonly.equals("")){
			request.setAttribute("msg", "提醒类型不能为空!");
			return "administrative/setReminding";
		}
		
		try {
			if(isonly.equals("1")){
				if(handleTime == null || handleTime.equals("")){
					request.setAttribute("msg", "处理时间不能为空!");
					return "administrative/setReminding";
				}
				assetsReminding.setHandleTime(format.parse(handleTime).getTime());
				assetsReminding.setEndtime(format.parse(handleTime).getTime());
			}else{
				assetsReminding.setRemindingCycle(Integer.parseInt(remindingCycle));
				assetsReminding.setRemindingInterval(Integer.parseInt(remindingInterval));
				Calendar endcal = Calendar.getInstance();
				if(remindingCycle.equals("1")){
					endcal.add(Calendar.MONTH, 6);
					assetsReminding.setEndtime(endcal.getTime().getTime());
				}else if(remindingCycle.equals("2")){
					endcal.add(Calendar.YEAR, 1);
					assetsReminding.setEndtime(endcal.getTime().getTime());
				}else if(remindingCycle.equals("3")){
					endcal.add(Calendar.YEAR, 3);
					assetsReminding.setEndtime(endcal.getTime().getTime());
				}else if(remindingCycle.equals("4")){
					endcal.add(Calendar.YEAR, 50);
					assetsReminding.setEndtime(endcal.getTime().getTime());
				}
				
				if(remindingInterval.equals("1")){
					Calendar cal = Calendar.getInstance();
					cal.add(cal.MONTH, 1);
					assetsReminding.setHandleTime(cal.getTime().getTime());
				}else if(remindingInterval.equals("2")){
					Calendar cal = Calendar.getInstance();
					cal.add(cal.MONTH, 2);
					assetsReminding.setHandleTime(cal.getTime().getTime());
				}else if (remindingInterval.equals("3")) {
					Calendar cal = Calendar.getInstance();
					cal.add(cal.MONTH, 3);
					assetsReminding.setHandleTime(cal.getTime().getTime());
				}else if(remindingInterval.equals("4")){
					Calendar cal = Calendar.getInstance();
					cal.add(cal.MONTH, 6);
					assetsReminding.setHandleTime(cal.getTime().getTime());
				}
			}
			if(assetsReminding.getHandleTime() == null){
				request.setAttribute("msg", "处理时间不符合规则");
				return "administrative/setReminding";
			}
			assetsReminding.setCreateTime(new Date().getTime());
			assetsRemindingService.addAssetsReminding(assetsReminding);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "设置失败!");
			return "administrative/setReminding";
		}
		request.setAttribute("msg", "设置成功!");
		return "administrative/setReminding";
	}
	
	/**
	 * 设置提醒界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/setreminding",method=RequestMethod.GET)
	public String setreminding(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		request.setAttribute("asid", id);
		return "administrative/setReminding";
	}
	/**
	 * 查看提醒界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selreminding",method=RequestMethod.GET)
	public String selreminding(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		TAssetsReminding assetsReminding = assetsRemindingService.findRemindingById(Integer.parseInt(id));
		String reminding = "";
		String remindingTime = null;
		String createTime = format.format(new Date(assetsReminding.getCreateTime()));
		if (assetsReminding.getHandleTime() != null) {
			remindingTime = format.format(new Date(assetsReminding.getHandleTime()));
		}
		String content = assetsReminding.getRemindingContent();
		if(assetsReminding.getRemindingType() == 1){
			reminding = "设置时间:"+createTime+";提醒时间:"+remindingTime+";提醒内容:"+content+";";
		}else{
			int cycle = assetsReminding.getRemindingCycle();
			int interval = assetsReminding.getRemindingInterval();
			String cycleString = null;
			String intervalString = null;
			if(cycle == 1){
				cycleString = "半年";
			}else if (cycle == 2) {
				cycleString = "一年";
			}else if (cycle == 3) {
				cycleString = "三年";
			}else if (cycle == 4) {
				cycleString = "无限制";
			}
			if(interval == 1){
				intervalString = "一个月";
			}else if (interval == 2) {
				intervalString = "两个月";
			}else if (interval == 3) {
				intervalString = "三个月";
			}else if (interval == 4) {
				intervalString = "半年";
			}
			reminding = "设置时间:"+createTime+";提醒周期:"+cycleString+";提醒间隔:"+intervalString+";下次提醒时间:"+remindingTime+";提醒内容:"+content+";";
		}
		request.setAttribute("reminding", reminding);
		return "administrative/selReminding";
	}
	/**
	 * 取消提醒
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delreminding",method=RequestMethod.POST)
	public String delreminding(HttpServletRequest request, Model model){
		String reid = request.getParameter("id");
		if(reid != null && !reid.equals("")){
			TAssetsReminding reminding = new TAssetsReminding();
			reminding.setStatus(2);
			reminding.setId(Integer.parseInt(reid));
			assetsRemindingService.updateAssetsReminding(reminding);
		}
		return index(request, model);
	}
	
	/**
	 * 资产获取员工列表信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getUser",method=RequestMethod.POST)
	public String getUser(HttpServletRequest request, Model model){
		String name = request.getParameter("name");
		String jobnumber = request.getParameter("jobnumber");
		String number = request.getParameter("number");
		int pageSize = 5;
		try {
			if (name!=null&&name.length() > 0) {
				name = URLDecoder.decode(URLDecoder.decode(name, "UTF-8"),"UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
		}
		request.setAttribute("name", name);
		request.setAttribute("jobnumber", jobnumber);
		request.setAttribute("number", number);
		// 获取当前页数
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int organizationid=(int) request.getSession().getAttribute("organizationid");
		int totalNum = userService.selectTUserNum(name, null, null, null, jobnumber,organizationid);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<TUser> list=userService.selectTUserList(name, null, null, null,jobnumber, organizationid,(currentPage - 1) * pageSize, pageSize);
		request.setAttribute("totalNum", totalNum);
		request.setAttribute("list", list);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalNum", totalNum);
        request.setAttribute("totalPage", totalPage);
		return outboundAssets(request, model);
	}
	
	/**
	 * 出库明细
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/outdetail",method=RequestMethod.GET)
	public String outdetail(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		int pageSize = 10;
		request.setAttribute("id", id);
		// 获取当前页数
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int totalNum = assetsdetailService.getTAssetsdetailNum(Integer.valueOf(id));
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<TAssetsdetail> list=assetsdetailService.getTAssetsdetailList(Integer.valueOf(id), (currentPage - 1) * pageSize, pageSize);
		request.setAttribute("totalNum", totalNum);
		request.setAttribute("list", list);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalNum", totalNum);
        request.setAttribute("totalPage", totalPage);
		return "administrative/outdetail";
	}
}
